
/*
Copyright 2009 Peter Ekler, Akos Ludanyi, Budapest University of Technology and Economics, Department of Automation and Applied Informatics, Applied Mobile Research Group (AMORG) http://amorg.aut.bme.hu

This file is part of MobTorrent.

MobTorrent is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MobTorrent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MobTorrent.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package network;

import Logger.MTLogger;
import data.MTPreferences;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author Tyrael
 */
public class MTUpdateManager
{
    public interface MTUpdateManagerListener {
        public abstract void newUpdatesAvailable();
        public abstract void noNewUpdate();
    }

    private MTUpdateManagerListener updateManagerListener;

    public MTUpdateManager(MTUpdateManagerListener aUpdateManagerListener)
    {
        updateManagerListener = aUpdateManagerListener;
    }

    private class SearchUpdateTask extends Thread
    {
        public void run()
        {
            try {
                MTLogger.writeLine("Trying to update.");
                HttpConnection hc = (HttpConnection)Connector.open(MTPreferences.MobTorrentVersionCheckURL);
                InputStream is = hc.openInputStream();

                MTLogger.writeLine("Connected to the update server.");
                int inputChar;
                ByteArrayOutputStream bf = new ByteArrayOutputStream();
                while ((inputChar = is.read()) != -1)
                {
                    bf.write(inputChar);
                }

                int version = Integer.parseInt(bf.toString());
                MTLogger.writeLine("Current version: " +
                    MTPreferences.MobTorrentVersion + " version on server: " + version);
                if (version == MTPreferences.MobTorrentVersion)
                {
                    updateManagerListener.noNewUpdate();
                }
                else
                {
                    updateManagerListener.newUpdatesAvailable();
                }

                // close phase
                try {
                    is.close();
                    hc.close();
                } catch (Exception ex) {
                    MTLogger.writeLine("Search for update failed: "+ex.getMessage());
                }
            } catch (Exception ex) {
                MTLogger.writeLine("Search for update failed: "+ex.getMessage());
            }
        }
    }

    public void searchForUpdates()
    {
        new SearchUpdateTask().start();
    }
}

